package com.hospital.pharmacy.impl;

import com.alibaba.fastjson.JSON;
import com.hospital.pharmacy.dao.*;
import com.hospital.pharmacy.entity.Inventory;
import com.hospital.pharmacy.entity.Medicine;
import com.hospital.pharmacy.service.MedicineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class MedicineServiceImpl implements MedicineService{
 @Autowired
 MedicineMapper medicineMapper;
    @Autowired
    PurchaseMapper purchaseMapper;
    @Autowired
    InventoryMapper inventoryMapper;
    @Autowired
    MedicineTypeMapper medicineTypeMapper;
    @Autowired
    SuppilerMapper suppilerMapper;
    @Autowired
    OverdueRecordMapper overdueRecordMapper;
    @Autowired
    SalesRecordMapper salesRecordMapper;

/*
    * @Description: 插入药品
    * @Param: [medicine]
    * @return: void
    * @Author: 杨mz
    * @Date: 2018/12/4
    */

    //显示查找到的药品信息
    public Map getAll(Medicine medicine){
        Map<String,String>map;

            map=new HashMap<>();
            map.put("id",medicine.getId().toString());
            map.put("mdcName",medicine.getMdcName());
            map.put("mdcUnit",medicine.getMdcUnit());
            map.put("mdcSpec",medicine.getMdcSpec());
            map.put("supName",suppilerMapper.selectByPrimaryKey(medicine.getMdcSupplierId()).getSupName());
            map.put("type",medicineTypeMapper.selectByPrimaryKey(medicine.getMdcTypeId()).getMdcTypeName());
            map.put("company",medicine.getMdcCompany());
            map.put("remark",medicine.getMdcRemark());

        return map;
    }

//插入药品
    @Override
    public String insertMed(String num, String name, String unit, String spec, Integer type, Integer supplier, String company, String remark){
       try {

           Medicine medicine = new Medicine();
           medicine.setMdcCompany(company);
           medicine.setMdcName(name);
           medicine.setMdcNum(num);
           medicine.setMdcRemark(remark);
           medicine.setMdcSpec(spec);
           medicine.setMdcSupplierId(supplier);
           medicine.setMdcUnit(unit);
           medicine.setMdcTypeId(type);
           medicineMapper.insert(medicine);

           return getAllMed();

       }catch (Exception e){
           e.printStackTrace();
           return JSON.toJSONString("插入药品错误");
       }
    }
    //根据ID删除药品
@Override
    public String deleteMed(Integer id){
        try {
            if (inventoryMapper.selectByMdcId(id)!=null){
                if (salesRecordMapper.selectSaleByIvtrId(inventoryMapper.selectByMdcId(id).getId())!=null){
                    salesRecordMapper.deleteByIvtrId(inventoryMapper.selectByMdcId(id).getId());

                }
                inventoryMapper.deleteByPrimaryKey(inventoryMapper.selectByMdcId(id).getId());
            }
             if (overdueRecordMapper.selectOverByMedId(id)!=null){
                 overdueRecordMapper.deleteByMedId(id);
             }
            if (purchaseMapper.selectPurchaseByMedId(id)!=null){
                purchaseMapper.deleteByMedId(id);
            }

            //库存，过期，进货，出货
            medicineMapper.deleteByPrimaryKey(id);
            return getAllMed();
        }catch (Exception e){
            e.printStackTrace();
            return "删除药品错误";
        }
}
//根据id得到药品
@Override
    public String getOneById(Integer id){
    return JSON.toJSONString(medicineMapper.selectByPrimaryKey(id));
}

//根据id修改药品
@Override
    public String update(Integer id, String num, String name, String unit, String spec, String company, String remark){
      try {

          Medicine medicine = medicineMapper.selectByPrimaryKey(id);
          medicine.setMdcCompany(company);
          medicine.setMdcName(name);
          medicine.setMdcNum(num);
          medicine.setMdcRemark(remark);
          medicine.setMdcSpec(spec);
          medicine.setMdcUnit(unit);
          medicineMapper.updateByPrimaryKeySelective(medicine);
          return getAllMed();
      }catch (Exception e){
          e.printStackTrace();
          return "修改错误";
      }
}
//查找所有药品
@Override
    public String getAllMed(){
               List<Map<String,String>>resaultmap=new ArrayList<>();
               List<Medicine>medicines=medicineMapper.selectAllMed();
               for (Medicine medicine:medicines){
                   resaultmap.add(getAll(medicine));
               }
               return JSON.toJSONString(resaultmap);
}
//根据药品名称得到药品
@Override
    public String getMedByMedName(String med_name){
        try {
            List<Map<String,String>>resaultmap=new ArrayList<>();
            List<Medicine> medicines = medicineMapper.selectByMdcName(med_name);
            for (Medicine medicine:medicines){
                resaultmap.add(getAll(medicine));
            }
            return JSON.toJSONString(resaultmap);
        }catch (Exception e){
            e.printStackTrace();
            return JSON.toJSONString("查找错误");
        }
}





/*
 @Override
    public String getAll() {
        AdminUserExample example = new AdminUserExample();
//        return JSON.toJSONString(adminUserMapper.selectByExample(example));
        List<AdminUser> adminUsers = adminUserMapper.selectByExample(example);
        //userNum
        //userName
        List<Map<String,String>> resultMap = new ArrayList<>();
        Map<String,String> map;
        for (AdminUser user : adminUsers) {
            map = new HashMap<>();
            map.put("userNum",user.getAdNum());
            map.put("userName",user.getAdName());
            resultMap.add(map);
        }
        return JSON.toJSONString(resultMap);
    }*/





}
